package yogiro.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import yogiro.service.NoticeService;
import yogiro.to.MemberTO;
import yogiro.to.NoticeCommentTO;
import yogiro.to.NoticeListTO;
import yogiro.to.NoticeTO;


public class NoticeController extends MultiActionController{
	private NoticeService service;
	public void setNoticeService(NoticeService service){
		this.service=service;
	}

	public ModelAndView noticeListController(HttpServletRequest request, HttpServletResponse response,HttpSession session)throws ServletException, IOException{
		ModelAndView mv=null;
		
		String pageTmp = request.getParameter("page");
		System.out.println("모냐??"+pageTmp);
		int page = 1;
		try{
			page = Integer.parseInt(pageTmp);
			System.out.println(page);
		}catch(Exception e){}
		
		try {
			NoticeListTO lto = service.getBoardListByPage(page);
			System.out.println("lto"+lto);
			return new ModelAndView("notice/notice_list","list",lto);
		} catch (SQLException e) {
			System.out.println("에러발생");
			e.printStackTrace();
		}
			
		return mv;
	}
	public ModelAndView WriteContentController(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		HttpSession session = request.getSession();
		MemberTO mto = (MemberTO) session.getAttribute("mto");
		String title=request.getParameter("title");
		String writer= mto.getMemberName();
		String content=request.getParameter("content");
		String mid=request.getParameter("mid");
		NoticeTO nto=new NoticeTO(title,content,writer, mid);
		ModelAndView mv=null;
		try {
			service.writeContent(nto);
			System.out.println("ok????");
			mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+nto.getNoticeNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return mv;
	}
	public ModelAndView GetContentController(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView mv = null;
		try{
			int no = Integer.parseInt(request.getParameter("no"));
			System.out.println("넘버값"+no);
			
			NoticeTO nto = service.getContentByNO(no); // 보드꺼
			System.out.println("뭐가나옴"+nto);
			System.out.println("암");
			
		}catch(Exception e){}	
		return mv;
	}
	public ModelAndView deleteContentController(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView mv = null;
		
		System.out.println("외안대 지우기");
			String page = request.getParameter("page");//paging
			
			int no = Integer.parseInt(request.getParameter("no"));
			
			System.out.println(page);
			
			System.out.println(no);
			
			try {
				service.deleteContentByNO(no);
				System.out.println("지우러가요");
				mv = new ModelAndView("redirect:/notice.do?method=noticeListController");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return mv;
	}
	public ModelAndView ModifyFormController(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView mv = null;
		
			int no = Integer.parseInt(request.getParameter("no"));
			//2. B.L 처리
			System.out.println(no);
			
			try {
				NoticeTO nto = service.getContentByNoForForm(no);
				System.out.println(nto);
				mv = new ModelAndView("notice/notice_modify", "nto", nto);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
		return mv;
	}

	public ModelAndView ModifyContentController(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ModelAndView mv = null;
	
	

	System.out.println("글수정진짜");
		int noticeNo = Integer.parseInt(request.getParameter("no"));
		String noticeTitle = request.getParameter("title");
		String noticeWriter = request.getParameter("writer");
		System.out.println("noticeTitle"+noticeTitle);
		System.out.println("writer"+noticeWriter);
		String noticeContent = request.getParameter("content");
		System.out.println("noticeContent"+noticeContent);
		int noticeViewcount = Integer.parseInt(request.getParameter("viewcount"));
	     NoticeTO nto = new NoticeTO(noticeNo, noticeTitle, noticeContent, noticeWriter, noticeViewcount);
		

		System.out.println("수정에와서"+nto);
		try {
			service.modifyContent(nto);
			System.out.println(nto);
			System.out.println("날라감?");
			mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+nto.getNoticeNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
	return mv;
	}
	//삭제하는 메소드
		public ModelAndView deleteComment(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv = null;
			int commentNo = Integer.parseInt(request.getParameter("commentNo"));
			System.out.println("코멘트넘버"+commentNo);
			int noticeNo = Integer.parseInt(request.getParameter("noticeNo"));
			System.out.println("게시물넘버"+noticeNo);
			try {
				service.deleteComment(commentNo, noticeNo);
				System.out.println("삭제됫습니당");
				mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+noticeNo);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return mv;
		}
		public ModelAndView selectAllInfo(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			ArrayList<NoticeCommentTO> list1=null;
			System.out.println("야");
			
			
			int no =Integer.parseInt(request.getParameter("no"));
				try {
					NoticeTO nto=service.getContentByNO(no);
					list1=service.selectCommentByNO(no);
					for(Object j:list1){
						System.out.println(j);
					}
					System.out.println("nto"+nto);
					NoticeListTO list=new NoticeListTO(list1,nto);
					mv=new ModelAndView("notice/notice_show","list",list);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return mv;
		}
		public ModelAndView searchYo(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			System.out.println("굿");
			ArrayList<NoticeTO> list=null;
			String txt=request.getParameter("txt");
			System.out.println("txt"+txt);
			String key=request.getParameter("search");
			System.out.println(key);
			
			int page = 1;
			System.out.println("페이지"+page);
			if(key.equals("tits")){
				try {
					NoticeListTO lto = service.getBoardListByPage1(page,txt);
				
					
					System.out.println("tits 첫번쨰꺼");
					
					return new ModelAndView("notice/notice_list","list",lto);
				} catch (SQLException e) {
					
					e.printStackTrace();
				}
			}else if(key.equals("wris")){
				
				try {
					NoticeListTO lto = service.getBoardListByPage2(page,txt);
				
					
					
					System.out.println("wris로감");
					return new ModelAndView("notice/notice_list","list",lto);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			return mv;
		}
		public ModelAndView NextFormController(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			System.out.println("왓냐");
			int no=Integer.parseInt(request.getParameter("no"));
			try {
				int end=service.byMax();
				if(end==no){
					mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+no);
				}if(end!=no){
				int ok=service.byNextNO(no);
				mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+ok);
				}
			} catch (SQLException e) {
			}
			
			
			return mv;
		}
		public ModelAndView PreFormController(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			System.out.println("왓냐2");
			int no=Integer.parseInt(request.getParameter("no"));
			try {
				int end=service.byMin();
				if(end==no){
					mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+no);
				}if(end!=no){
				int ok=service.byPreNO(no);
				mv = new ModelAndView("redirect:/notice.do?method=selectAllInfo&no="+ok);
				}
			} catch (SQLException e) {
			}
			
			
			return mv;
		}
		public ModelAndView checkDel(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			System.out.println("처음");
			String []del=request.getParameterValues("ch");
			for(int i=0;i<del.length;i++){
				String d=del[i];
				int j=Integer.parseInt(d);
				try {
					service.deleteContentByNO(j);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			mv = new ModelAndView("redirect:/notice.do?method=noticeListController");
			return mv;
		}
		
		public ModelAndView dateSearch(HttpServletRequest request, HttpServletResponse response){
			ModelAndView mv=null;
			System.out.println("굿");
			String a=request.getParameter("startDate");
			
			String b=request.getParameter("endDate");
			
			StringBuffer sb= new StringBuffer(a);
			sb.append("000000");
			String st=sb.toString();
			
		
			StringBuffer ab= new StringBuffer(b);
			ab.append("000000");
			String en=ab.toString();
			
			ArrayList<NoticeTO> list=null;
			int page = 1;
			try {
				NoticeListTO lto=service.getBoardListByPageDate(page,st,en);
				return new ModelAndView("notice/notice_list","list",lto);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return mv;
		}
		
		public String writeNotice(HttpServletRequest request, HttpServletResponse response) throws Exception{
			return "notice/notice_write";
		}
	}